package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import school.domain.Notice;
import school.mapper.NoticeMapper;
import school.support.BaseController;
import school.support.pojo.OrdersPojo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Controller
@RequestMapping("/admin/notice")
public class AdminNoticeController extends BaseController {

    @Autowired
    private NoticeMapper noticeMapper;

    @RequestMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") Integer pageNum, Notice notice) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Notice> pageInfo = new PageInfo<>(noticeMapper.selectByExample(new Example(Notice.class)), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "admin/notice/list";
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        model.addAttribute(notice);
        return "admin/notice/detail";
    }

    @GetMapping("add")
    public String add() {
        return "admin/notice/add";
    }

    @PostMapping("add")
    public String add(Notice notice, MultipartFile icon) {
        notice.setImg(saveFile(icon));
        notice.setAddtime(new Date());
        noticeMapper.insert(notice);
        return "redirect:list";
    }




    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        noticeMapper.deleteByPrimaryKey(id);
        return refresh();
    }
}
